//
//  PhotoBrowserAnimator.swift
//  Weibo8023
//
//  Created by styshy on 15/12/21.
//  Copyright © 2015年 sz. All rights reserved.
//

import UIKit

/**
 *  照片浏览器专场动画
 */
protocol PhotoBrowserPresentDelegate: NSObjectProtocol{
    // 参与专场动画的图像视图
    func imageViewForPresent(indexPath:NSIndexPath) -> UIImageView
    // 转场动画的起始位置
    func photoBrowserFromRect(indexPath:NSIndexPath) -> CGRect
    
    // 转场动画的结束位置
    func photoBrowserToRect(indexPath:NSIndexPath) -> CGRect
}

// MARK: - 照片浏览器解除转场代理
protocol PhotoBrowserDismissDelegate: NSObjectProtocol{
    // 解除转场动画的图像模型
    func imageViewForDismiss() -> UIImageView
    // 解除专场动画的图像索引
    func indexPathForDismiss() -> NSIndexPath
}

/// 专场动画代理对象
class PhotoBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate{
    
    // 展现转场代理对象
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    
    weak var dismissDelegate: PhotoBrowserDismissDelegate?
    
    
    // 展现动画对应的缩影
    var indexPath:NSIndexPath?
    
    // 设置代理参数
    func setDelegateParams(presentDelegate:PhotoBrowserPresentDelegate,
        dismissDelegate:PhotoBrowserDismissDelegate,
        indexPath:NSIndexPath){
            
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexPath = indexPath
    }
    
    // 是否展现的标志
    var isPresented  = false
    
    /// 返回`提供展现动画`的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        return self
    }
    
    /// 返回`提供解除转场动画`的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        return self
    }


}

// MARK: - 提供具体专场动画的实现
extension PhotoBrowserAnimator:UIViewControllerAnimatedTransitioning{
    
    // 动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    // 真正提供辗转动画的方法
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
//        
//        
//        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
//        print("toView = \(toVC)")
//        
//        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
//        print("fromVC = \(fromVC)")
// 
//        
        
        // 显示转场动画还是解除动画
        isPresented ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
    
    // 提供专场动画
    private func presentAnimation(transitionContext:UIViewControllerContextTransitioning){
        guard let presentDelegate = presentDelegate,indexPath = indexPath else{
            return
        }
        
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        
        transitionContext.containerView()?.addSubview(toView!)
        
        toView?.alpha = 0
        
        // 转场动画图像视图
        let iv = presentDelegate.imageViewForPresent(indexPath)
        
        print(iv)
        UIApplication.sharedApplication().keyWindow?.addSubview(iv)
        // 设置起始位置
        iv.frame = presentDelegate.photoBrowserFromRect(indexPath)
        // 将视图添加到容器
        transitionContext.containerView()?.addSubview(iv)
        
        // 转场动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            toView?.alpha = 1.0
            iv.frame = presentDelegate.photoBrowserToRect(indexPath)
            iv.alpha = 0
            }) { (_) -> Void in
                iv.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
    // 解除转场动画
    private func dismissAnimation(transitionContext:UIViewControllerContextTransitioning){
        guard let presentDelegate = presentDelegate,dismissDelegate = dismissDelegate else{
            return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        // 专场动画中的大米视图
        let iv = dismissDelegate.imageViewForDismiss()
        let indexPath = dismissDelegate.indexPathForDismiss()
        
        transitionContext.containerView()?.addSubview(iv)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            fromView?.alpha = 0
            iv.frame = presentDelegate.photoBrowserFromRect(indexPath)
            }) { (_) -> Void in
                iv.removeFromSuperview()
                fromView?.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
        
    
    }
}
